package valuetypes;

import java.nio.ByteBuffer;

import exception.DBValueException;



public class NumericValue extends DBValue {
	
	public static final int NUMERIC_SIZE = 4;
	private int value;
	
	public NumericValue( byte[] data, int offset)
			throws DBValueException {
		super(NUMERIC_SIZE, data, offset);
	}

	public NumericValue(){
		super(NUMERIC_SIZE);
		setValue(0);
	}

	public NumericValue(int value) {
		super(NUMERIC_SIZE);
		setValue(value);
	}
	
	public void setValue(int value) {
		ByteBuffer bb = ByteBuffer.allocate(NUMERIC_SIZE);
		bb.putInt(value);
		try {
			setData(bb.array());
		} catch (DBValueException e) {
			//Should never go wrong.
			e.printStackTrace();
		}
	}

	public int getValue(){
		return value;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof NumericValue){
			NumericValue nv = (NumericValue)obj;
			if(nv.value == this.value)return true;
			else return false;
		}
		else if(obj instanceof Integer){
			return getValue() == (Integer)obj;
		}
		
		return false;
	}
	
	@Override
	public String toString() {
		return "" + value;
	}
	
	@Override
	protected void onDataChanged(byte[] oldData, byte[] newData) {
		ByteBuffer bb = ByteBuffer.wrap(newData);
		value = bb.getInt();
	}
}
